<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="项目编号:" prop="xmBh">
            <el-input
              v-model="queryParams.xmBh"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称:" prop="xmName">
            <el-input
              v-model="queryParams.xmName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请部门:" prop="sqBm">
            <el-input
              v-model="queryParams.sqBm"
              placeholder="请输入申请部门"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="支出选项:" prop="zcXx">
            <el-select v-model="queryParams.zcXx" placeholder="请选择支出选项" clearable>
              <el-option
                v-for="dict in dict.type.lstb_zcxx"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="项目实施状态:" prop="xmSsZt">
            <el-select v-model="queryParams.xmSsZt" placeholder="请选择项目实施状态" clearable>
              <el-option
                v-for="dict in dict.type.lstb_xmsszt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="研发成果证书号:" prop="yfCgZsh">
            <el-input
              v-model="queryParams.yfCgZsh"
              placeholder="请输入研发成果证书号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fztz:lstb:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              class="fixedTableTwo" height="300" v-loading="loading" :data="lstbList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="xmName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.xmName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="xmBh"/>
      <el-table-column show-overflow-tooltip label="项目截止时间" align="center" prop="zTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目延期" align="center" prop="yqCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleXmYqCk(scope.row)"
          >
            <el-link v-if="scope.row.yqCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.yqCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申请部门" align="center" prop="sqBm"/>
      <el-table-column show-overflow-tooltip label="支出选项" align="center" prop="zcXx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_zcxx" :value="scope.row.zcXx"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目实施状态" align="center" prop="xmSsZt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_xmsszt" :value="scope.row.xmSsZt"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目类型" align="center" prop="xmLx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_xmlx" :value="scope.row.xmLx"/>
        </template>
      </el-table-column>
      <el-table-column label="立项信息" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleLxXxClick(scope.row)"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="研发活动记录" align="center" prop="clxhtbId">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleClXhCk(scope.row)"
          >
            <el-link v-if="scope.row.clxhtbId != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.clxhtbId == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="合同变更" align="center" prop="htBgCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleHtBgCk(scope.row)"
          >
            <el-link v-if="scope.row.htBgCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.htBgCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="vxe-icon-paste"
            @click="handleLsCk(scope.row)"
          >流水查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="vxe-icon-paste"
            @click="handleYbFy(scope.row)"
          >月报费用
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="临时操作" align="center" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="vxe-icon-edit"
            @click="handleAddXmLs(scope.row)"
          >流水编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 科研项目选择 -->
    <HtXzKt ref="ktXz"/>

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <Xmxq ref="xmxq"/>

    <!-- 项目流水 -->
    <Xmls ref="xmlS"/>

    <!-- 合同变更查看 -->
    <HtBgCk ref="htBgCk"/>

    <!-- 项目延期查看 -->
    <XmYqCk ref="xmYqCk"/>

    <!-- 流水填报查看 -->
    <LsTbXq ref="lsTbXq"/>

    <!-- 材料消耗查看页面 (研发活动记录查看) -->
    <ClXhCk ref="clXhCk"/>

    <!-- 月报费用 -->
    <YbFy ref="ybFy"/>

    <!-- 流水查看 -->
    <LsCk ref="lsCk"/>
  </div>
</template>

<script>
import {listLstb} from "@/api/fztz/lstb";
import HtXzKt from "@/views/components/ktXz/HtXzKt.vue";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq.vue";
import Xmls from "@/views/fztz/xmls/index.vue";
import HtBgCk from "@/views/xmgl/htgl/htbz/components/HtBgCk.vue";
import XmYqCk from "@/views/xmgl/htgl/htbz/components/XmYqCk.vue";
import LsTbXq from "@/views/fztz/lstb/components/LsTbXq.vue";
import ClXhCk from "@/views/fztz/components/clxhck/ClXhCk.vue";
import YbFy from "@/views/fztz/lsck/ybfy/YbFy.vue";
import LsCk from "@/views/fztz/lsck/lsck/LsCk.vue";

export default {
  name: "Lstb",
  components: {LsCk, YbFy, ClXhCk, LsTbXq, XmYqCk, HtBgCk, Xmls, Xmxq, HtXzKt},
  dicts: ['lstb_xmsszt', 'lstb_zcxx', 'lstb_xmlx'],
  provide() {
    return {
      updateXmsbId: (val) => {
        this.form.xmBh = val.ktBh
        this.$refs['formDialog'].validateField('xmBh');
        this.form.xmName = val.ktName
        this.$refs['formDialog'].validateField('xmName');
        this.form.xmsbId = val.xmsbId
        this.form.sqBm = val.zrBm
      }
    }
  },
  data() {
    return {
      getLsTbId: null, //从后端获取到的流水填报ID
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流水填报表格数据
      lstbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmBh: null,
        xmName: null,
        xmsbId: null,
        sqBm: null,
        zcXx: null,
        xmSsZt: null,
        yfCgZsh: null,
        xmLx: null,
        idWhere: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xmBh: [{required: true, message: "项目编号不能为空", trigger: "blur"}],
        xmName: [{required: true, message: "项目名称不能为空", trigger: "blur"}],
        zcXx: [{required: true, message: "支出选项不能为空", trigger: "change"}],
        xmSsZt: [{required: true, message: "项目实施状态不能为空", trigger: "change"}],
        xmLx: [{required: true, message: "项目类型不能为空", trigger: "change"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //流水查看
    handleLsCk(row) {
      this.$refs.lsCk.handleOpen(row)
    },
    //月报费用
    handleYbFy(row) {
      this.$refs.ybFy.handleOpen(row)
    },
    //材料消耗查看页面 (研发活动记录查看)
    handleClXhCk(row) {
      this.$refs.clXhCk.handleOpen(row)
    },
    //打开流水填报详情
    handleButtonClick(row) {
      this.$refs.lsTbXq.handleOpen(row)
    },
    //点击项目延期打开项目延期查看弹窗
    handleXmYqCk(row) {
      this.$refs.xmYqCk.handleOpen(row);
    },
    //点击变更次数打开合同变更查看弹窗
    handleHtBgCk(row) {
      this.$refs.htBgCk.handleOpen(row);
    },
    //打开项目流水弹窗页面
    handleAddXmLs(row) {
      this.$refs.xmlS.handleOpen(row)
    },
    //打开立项信息详情
    handleLxXxClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    // 打开选择科研项目弹窗
    selectKt() {
      this.$refs.ktXz.handleOpen();
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询流水填报列表 */
    getList() {
      this.loading = true;
      listLstb(this.queryParams).then(response => {
        this.lstbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lstbId: this.getLsTbId,
        xmBh: null,
        xmName: null,
        xmsbId: null,
        sqBm: null,
        zcXx: null,
        xmSsZt: null,
        yfCg: null,
        yfCgZsh: null,
        xmLx: '自主研发',
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lstbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // selectYbFyList(111, '2024', '锁定').then(res => {
    //   console.log(res)
    // })
    /** 导出按钮操作 */
    handleExport() {
      this.download('fztz/lstb/export', {
        ...this.queryParams
      }, `lstb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
